package cn.yangliu.oauth2.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.io.Serializable;
import java.util.*;

/**
 * @author 杨柳
 * @contact
 * @date 2019-03-26 01:20
 * @desp 应用表
 */
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@Getter
@NoArgsConstructor
@Setter
@TableName("oauth2_client")
@ToString
public class Oauth2Client implements Serializable, ClientDetails {

    private static final long serialVersionUID = 1L;

    public Oauth2Client(String clientId) {
        this.clientId = clientId;
    }

    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 应用名称
     */
    private String name;

    /**
     * 应用id
     */
    private String clientId;

    /**
     * 应用密钥
     */
    private String clientSecret;

    /**
     * 应用描述
     */
    private String description;

    /**
     * 应用icon地址
     */
    private String iconUri;

    /**
     * 应用范围
     */
    private String scopes;

    /**
     * 应用所支持的授权模式,至少一个,多个用逗号隔开
     */
    private String grantTypes;

    /**
     * 认证令牌有效期COMMENT ,单位秒
     */
    private Integer accessTokenValiditySeconds;

    /**
     * 刷新令牌有效期COMMENT ,单位秒
     */
    private Integer refreshTokenValiditySeconds;

    /**
     * 是否受信任的,0,不受信,1受信
     */
    private String trusted;

    /**
     * 应用重定向地址
     */
    private String redirectUris;


    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 修改时间
     */
    private String updateTime;

    /**
     * 备注
     */
    private String remark;

    private String authorities;

    @Override
    public Set<String> getResourceIds() {
        return null;
    }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    @Override
    public Set<String> getScope() {
        return createSet(scopes);
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return createSet(grantTypes);
    }

    private LinkedHashSet<String> createSet(String s){
        LinkedHashSet<String> set = new LinkedHashSet<>();
        if (s != null) {
            String[] split = s.split(",");
           set.addAll(Arrays.asList(split));
        }
        return set;
    }



    @Override
    public Set<String> getRegisteredRedirectUri() {
        return createSet(redirectUris);
    }

    /**
     * 不能返回null 否则永远不能通过
     * 详情 org.springframework.security.core.userdetails.User#123
     * this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
     */
    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authoritiesList = new ArrayList<>();
        if (Objects.isNull(authorities)){
            return authoritiesList;
        }
        String[] split = authorities.split(",");

        for (String s : split) {
            authoritiesList.add(new SimpleGrantedAuthority(s));
        }
        return authoritiesList;
    }


    @Override
    public boolean isAutoApprove(String scope) {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        Map<String, Object> info = new HashMap<>(10);
        if (name != null) {
            info.put("name", name);
        }
        if (description != null) {
            info.put("desp", description);
        }
        if (iconUri != null) {
            info.put("icon_uri", iconUri);
        }
        return info;
    }
}
